﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input.CustomerinformationSinput;
using IntelligentAgriculture.Contracts.Dto.Output.CustomerinformationSOutput;
using IntelligentAgriculture.Contracts.Dto.Output.EarTagOutput;
using IntelligentAgriculture.Contracts.Dto.Output.PageOutput;
using IntelligentAgriculture.Contracts.Interface.ICustomerinformationService;
using IntelligentAgriculture.Domain.Interface.Customerinformations;
using IntelligentAgriculture.Domain.Interface.IEarTagReposiotry;
using MathNet.Numerics.LinearAlgebra.Factorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services.CustomerinformationService
{
    public class CustomerinformationsService : ICustomerinformationService
    {
        private readonly ICustomerinformationRepository _customerinformationRepository;
        private readonly IMapper _mapper;
        public CustomerinformationsService(ICustomerinformationRepository customerinformationRepository, IMapper mapper)
        {
            _customerinformationRepository = customerinformationRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 客户显示，查询，分页功能
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagesOutput<CustomerinformationShowOutput>> GetShow(CustomerinformationShowInput input)
        {
            var list = await _customerinformationRepository.GetShow();
            if (input.CustomerInformationId > 0)
            {
                list=list.Where(x=>x.CustomerInformationId== input.CustomerInformationId);
            }
            if(!string.IsNullOrEmpty(input.ChargePersonPhone))
            {
                list=list.Where(x=>x.ChargePersonPhone.Contains(input.ChargePersonPhone));
            }
            if(!string.IsNullOrEmpty(input.CustomerName))
            {
                list = list.Where(x => x.CustomerName.Contains(input.CustomerName));
            }
            if(!string.IsNullOrEmpty(input.CollectionFile))
            {
                list = list.Where(x => x.CollectionFile.Contains(input.CollectionFile));
            }
            if(!string.IsNullOrEmpty(input.CustomLabel))
            {
                list = list.Where(x => x.CustomLabel.Contains(input.CustomLabel));
            }
            if(input.UseCondition>0)
            {
                list = list.Where(x => x.UseCondition == input.UseCondition);
            }
            if(!string.IsNullOrEmpty(input.Market))
            {
                list = list.Where(x => x.Market.Contains(input.Market));
            }
            if(!string.IsNullOrEmpty(input.Salesman))
            {
                list = list.Where(x => x.Salesman.Contains(input.Salesman));
            }


            var result = new PagesOutput<CustomerinformationShowOutput>()
            {
                Total = list.Count(),
                PageSize = input.PageSize
            };

            var query = list.OrderByDescending(x => x.IdentityCard).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
            result.Data = _mapper.Map<List<CustomerinformationShowOutput>>(query);
            return result;


        }
    }
}
